﻿using System;
using System.Runtime.InteropServices;



namespace Piao
{
    class Win7ProcessIcon
    {
        ITaskbarList4 tastbar;

        public Win7ProcessIcon()
        {
            if (RunOnWin7())
            {
                tastbar = (ITaskbarList4)new CTaskbarList();
                tastbar.HrInit();
            }

        }


        public void SetProgress(IntPtr handle, ulong value, ulong maxvalue)
        {
            if (RunOnWin7())
                tastbar.SetProgressValue(handle, value, maxvalue);
        }


        public void SetState(IntPtr handle, TBPFLAG stats)
        {
            if (RunOnWin7())
                tastbar.SetProgressState(handle, stats);
        }


        private static bool RunOnWin7()
        {
            return (Environment.OSVersion.Version.Major > 6) ||
                      (Environment.OSVersion.Version.Major == 6 && Environment.OSVersion.Version.Minor >= 1);
        }
    }


    [ComImportAttribute()]
    [GuidAttribute("c43dc798-95d1-4bea-9030-bb99e2983a1a")]
    [InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
    internal interface ITaskbarList4
    {
        // ITaskbarList
        [PreserveSig]
        void HrInit();
        [PreserveSig]
        void AddTab(IntPtr hwnd);
        [PreserveSig]
        void DeleteTab(IntPtr hwnd);
        [PreserveSig]
        void ActivateTab(IntPtr hwnd);
        [PreserveSig]
        void SetActiveAlt(IntPtr hwnd);

        // ITaskbarList2
        [PreserveSig]
        void MarkFullscreenWindow(
            IntPtr hwnd,
            [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);

        // ITaskbarList3
        [PreserveSig]
        void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
        [PreserveSig]
        void SetProgressState(IntPtr hwnd, TBPFLAG tbpFlags);

        //the rest are ignored....
    }

    [GuidAttribute("56FDF344-FD6D-11d0-958A-006097C9A090")]
    [ClassInterfaceAttribute(ClassInterfaceType.None)]
    [ComImportAttribute()]
    internal class CTaskbarList { }


    internal enum TBPFLAG
    {
        TBPF_NOPROGRESS = 0,
        TBPF_INDETERMINATE = 0x1,
        TBPF_NORMAL = 0x2,
        TBPF_ERROR = 0x4,
        TBPF_PAUSED = 0x8
    }

}
